from PyQt5.QtWidgets import QWidget, QHBoxLayout, QLabel, QLineEdit
from PyQt5.QtCore import pyqtSignal
from bh_utils import logger

class BHCalc(QWidget):
    val_changed = pyqtSignal()
    def __init__(self, parent=None):
        super().__init__(parent)
        self.m_layout = QHBoxLayout()
        self.setLayout(self.m_layout)
        self.m_val = 0
        self.m_label = QLabel("=")
        self.m_line_edit = QLineEdit()
        self.m_line_edit.setPlaceholderText("Input formula")
        self.m_layout.addWidget(self.m_label)
        self.m_layout.addWidget(self.m_line_edit)
        self.m_line_edit.editingFinished.connect(self.calc)
        
    def calc(self):
        formula_str = self.m_line_edit.text()
        if(formula_str == ""):
            formula_str = "0"
        try:
            self.m_val = eval(formula_str)
            self.val_changed.emit()
        except Exception as e:
            logger.error(str(e))

    def setval(self, val:int):
        self.m_val = val
        self.m_line_edit.setText(str(val))

    def val(self) -> int:
        return self.m_val